package active

import (
	"errors"
	"gm/app/dto/active_dto"
	"gm/app/model/model"
	"gm/app/util/common"
	"time"
)

type ActiveService struct{}

func (this *ActiveService) GetActives(cid int) ([]*active_dto.NewPlayActiveManage, error)   {


	if 1<0{
		return nil,errors.New("23") 	//手动抛异常
		}

	var p []*active_dto.PlayActiveManage
	err := model.DB.Select("id,type,name,start_time,end_time").Where("cid = ? ", cid).Find(&p).Error
	if err != nil {
		return  nil,err
	}

	 otherValid := []*active_dto.NewPlayActiveManage{}
	 if len(p)>0{
		for _, value := range p {
			if value.Type<57{
				continue
			}

			var tmp active_dto.NewPlayActiveManage
			tmp.ID=value.ID
			tmp.Name=value.Name
			tmp.Type=value.Type
			tmp.StartTime=value.StartTime
			tmp.EndTime=value.EndTime
			otherValid = append(otherValid, &tmp)
		}
	}

	return  otherValid,nil
}
func (this *ActiveService) GetData(id int) (active_dto.Out, error) {
	var p []*active_dto.PlayActiveManageWeek
	var result = active_dto.Out{}
	nor := "0"
	res, _ := this.get_active_conf(id)
	if len(res) == 0 {
		return result, nil
	}

	monday := common.GetFirstDateOfWeek()                                                       //周一
	sunday := time.Unix(common.DateToTimestamp(monday)+active_dto.TIME, 0).Format("2006-01-02") //周日
	weekarea := monday + "-" + sunday

	err := model.DB.Select("id,left_money").Where("weekarea = ? AND cid = ? ", weekarea, 157).Find(&p).Error
	if err != nil {
		return result, nil
	}

	if len(p) > 0 {
		nor = p[0].LeftMoney
	} else {
		nor = "200"
	}
	result.ApplyMoney = nor
	result.Nums = "200"
	result.Imgs = "201"
	return result, nil
}

func (this *ActiveService) get_active_conf(id int) ([]*active_dto.Play_active_manage_conf_copy, error) {
	var p []*active_dto.Play_active_manage_conf_copy
	err := model.DB.Select("id").Where("id = ? AND enable = ? ", id, 1).Find(&p).Error
	if err != nil {
		return nil, err
	}
	return p, nil
}
